
from pytrends.request import TrendReq
import matplotlib.pyplot as plt
import time
import pandas as pd
import datetime as dt

def get_day_num(date):

    mo, d, y = map(int, date.split('/'))
    day_num = (dt.date(y, mo, d) - dt.date(y,1,1)).days
    return day_num

def get_day_str(num, year):
    day_str = (dt.datetime(year,1,1) + dt.timedelta(days=num)).strftime("%-m/%-d/%Y")
    return day_str

def get_data(term, geo, y_start, y_end, mo_start, mo_end, d_start, d_end, h_start, h_end):
    data = pred.get_historical_interest(term, year_start=y_start, month_start=mo_start, day_start=d_start, hour_start=h_start,
                                        year_end=y_end, month_end=mo_end, day_end=d_end, hour_end=h_end, cat=0, geo=area, gprop='', sleep=0)
    return data


def get_weeks(term, area, date_earliest, date_latest):
    day_start_num = get_day_num(date_earliest)
    day_end_num = get_day_num(date_latest)
    year = int(date_earliest.split('/')[2])
    all_data = []
    for i in range(1,(day_end_num-day_start_num+1)+1):
        mo_start, d_start, y_start = map(int, get_day_str(day_start_num, year).split('/'))
        mo_end, d_end, y_end = map(int, get_day_str(day_start_num+8, year).split('/'))
        d = get_data(term, area, y_start, y_end, mo_start, mo_end, d_start, d_end, 0, 0)
        all_data.append(d.iloc[:,0:1])
        if get_day_num(get_day_str(day_start_num+8, year)) == day_end_num:
            break
        else:
            day_start_num += 1
            time.sleep(10)
    return all_data

## RUN:

term = ["protest"]
area = 'US-MD-512'
date_earliest = '3/1/2015'
date_latest = '5/31/2015'  

pred = TrendReq(hl='en-US', tz=360) 
d = get_weeks(term, area, date_earliest, date_latest)
frame = pd.concat(d, axis=1)
means = frame.mean(axis=1)

frame.to_csv('../Data/trends_data_Mar_to_May.csv', index = ['date'], header=True)
